<template>
  <div class="newTimingTaskContainer">
    <div
      class="dialog animated"
      :class="open? 'fadeIn':'fadeOut'"
      v-show="show"
      :style="{visibility: (visible?'visible':'hidden')}"
    >
      <div class="panel animated" :class="open? 'zoomIn':''">
        <div class="title">
          {{title}}
          <div class="close" @click="cancel()"></div>
        </div>
        <el-form label-width="240px">
          <el-form-item label="bean名称" :rules="[
      { required: true, message: ''}]">
            <el-input
              ref="beanName"
              style="width:50%"
              clearable
              v-model="timingTaskData.beanName"
              placeholder="spring bean名称,如：testTask"
            ></el-input>
          </el-form-item>
          <el-form-item label="参数">
            <el-input
              ref="params"
              style="width:50%"
              clearable
              v-model="timingTaskData.params"
              placeholder="请输入参数"
            ></el-input>
          </el-form-item>
          <el-form-item label="cron表达式" :rules="[
      { required: true, message: ''}]">
            <el-input
              ref="cronExpression"
              style="width:50%;float: left;"
              clearable
              v-model="timingTaskData.cronExpression"
              placeholder="如：0 0 12 * *?"
            ></el-input>
            <div class="createCron" @click="createCron()">表达式生成</div>
          </el-form-item>
          <el-form-item label="备注">
            <el-input
              ref="remark"
              style="width:50%"
              clearable
              v-model="timingTaskData.remark"
              placeholder="请输入备注"
            ></el-input>
          </el-form-item>
        </el-form>
        <div class="btn_div">
          <div class="btn_sure" @click="sure()">确 认</div>
          <div class="btn_cancel" @click="cancel()">取 消</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
var vm;
export default {
  data: function() {
    return {
      show: false,
      open: false,
      visible: false,
      isAdd: true,
      title: "",
      timingTaskData: {}
    };
  },
  methods: {
    createCron() {
      window.open("http://www.bejson.com/othertools/cron/", "_blank");
    },
    init: function(data) {
      vm.show = true;
      setTimeout(function() {
        vm.open = true;
        vm.visible = true;
      }, 11);
      if (data == null || data == undefined) {
        vm.title = "新增定时任务";
        vm.isAdd = true;
        vm.timingTaskData = {
          beanName: "",
          params: "",
          cronExpression: "",
          status: 0,
          remark: ""
        };
      } else {
        vm.isAdd = false;
        vm.title = "编辑定时任务";
        vm.timingTaskData = data;
      }
    },
    sure: function() {
      var rules = [];
      let beanName = this.$refs.beanName;
      let cronExpression = this.$refs.cronExpression;
      rules.push({
        judge: tool.judge("classname", vm.timingTaskData.beanName),
        focus: beanName,
        tips: "请输入正确的bean名称"
      });
      rules.push({
        judge: vm.timingTaskData.cronExpression,
        focus: cronExpression,
        tips: "请输入cron表达式"
      });
      if (!tool.validate(rules)) {
        return;
      }
      vm.timingTaskData._wait = "";
      if (vm.isAdd) {
        api.scheduleAdd(vm.timingTaskData).then(function(data) {
          console.log(data);
          if (data.resultCode == 2000) {
            vm.$message({
              message: "新增成功",
              type: "success"
            });
            vm.$emit("callback", "添加成功");
            vm.cancel();
          } else {
            vm.$message.error(data.message);
          }
        });
      } else {
        api.scheduleUpdate(vm.timingTaskData).then(function(data) {
          console.log(data);
          if (data.resultCode == 2000) {
            vm.$message({
              message: "编辑成功",
              type: "success"
            });
            vm.$emit("callback", "");
            vm.cancel();
          } else {
            vm.$message.error(data.message);
          }
        });
      }
    },
    cancel: function() {
      //隐藏弹窗
      vm.open = false;
      vm.isAdd = true;
      vm.weekList = [];
      setTimeout(function() {
        vm.show = false;
        vm.visible = false;
      }, 500);
    }
  },
  created: function() {
    vm = this;
  }
};
</script>
<style lang="scss">
.newTimingTaskContainer {
  .el-form-item__label {
    font-size: 0.16rem;
    color: #666666;
  }
  .el-checkbox__label {
    font-size: 0.16rem;
    color: #666666;
  }
  .createCron {
    float: left;
    margin-left: 0.2rem;
    cursor: pointer;
    color: #3a85f8;
  }
  .createCron:hover {
    color: rgb(50, 108, 196);
  }
}
</style>
<style scoped src="@/style/module/dialog.css"></style>

